// pages/card1/card1.js
var app = getApp();
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isnull:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()
    app.checkUser(app.globalData.openid, this);
    
  },
  getData() {
    var that = this;
    wx.request({
      url: app.globalData.host + 'index.php/api/goods/goodsInfo?admin_id=' + app.globalData.admin_id + '&type=' + 67212 + '&code=3',
      success: function (e) {

        that.setData({
          items: e.data.result[0]
        })
        that.getorder()
        var article = e.data.result[0].goods_content;
        WxParse.wxParse('article', 'html', article, that, 8);
      }
    })
  },
  getorder() {
    var that = this;
    wx.request({
      url: app.globalData.host + 'index.php/api/goods/findorder?admin_id=' + app.globalData.admin_id + '&openid=' + app.globalData.openid,
      method: "POST",
      success: function (e) {
        if (e.data == '') {
          that.setData({
            isnull: true
          })
        } else {
          that.setData({
            order: e.data.data.filter(that.filterHeaderCatids),
            isnull: false
          })
          if (that.data.order.length == 0) {
            that.setData({
              isnull: true
            })
            return false
          }

          for(let i = 0;i < that.data.order.length;i++){
            that.data.order[i].f_10 = that.fordate(parseFloat(new Date(that.data.order[i].pre_time.replace(/-/g, '/')).getTime()) + 31536000000);
          }
          that.setData({
            order: that.data.order,
          })
        }

      }
    })
  },
  fordate(time){
    var date = new Date(time);
    let Y = date.getFullYear() + '-';
    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    let D = date.getDate() + ' ';
    let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
    return (Y + M + D + h + m + s);
  },
  tobuy() {
    var that = this;
    app.payFor(that.data.vipInfo.nickname, that.data.vipInfo.mobile, that.data.items.shop_price, "【年卡】" + that.data.vipInfo.nickname, function () {
    wx.request({
      url: 'https://xcxu.we-fs.com/index.php/api/goods/order1',
      method: 'post',
      data: {
        'name': that.data.vipInfo.nickname,
        'sj': that.data.vipInfo.mobile,
        'goods_name': "【年卡】" + that.data.vipInfo.nickname,
        'price': that.data.items.shop_price,
        // 'liuyan': e.currentTarget.dataset.liuyan,
        // 'dz': r.provinceName + r.cityName + r.countyName + r.detailInfo,
        'admin_id': app.globalData.admin_id,
        'goods_id': that.data.items.goods_id,
        'openid': app.globalData.openid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res.data)
        app.showPop('购买成功', true)
        that.getorder();
        setTimeout(function () {
          that.setData({
            loadingHidden: true
          })
        }, 1500)
      }
    })

    })
  },

  filterHeaderCatids(e){
    let date = parseFloat(new Date(e.pre_time.replace(/-/g, '/')).getTime());
    let nowdate = parseFloat(new Date().getTime());
    let year = parseFloat(31536000000);
    return (e.cat_id == 67212 && nowdate <= date + year)
  }



})